/*
 * ServerMain.java
 *
 * Created on 14. Juni 2008, 19:26
 */

package ads.server;

import javax.swing.JOptionPane;

import ads.exception.ServerRemoteException;
import ads.exception.ServerSpreadException;
import ads.util.ServerConfig;

/**
 * 
 * @author xian
 */
public class ServerMain extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ServerImpl server;
	ServerConfig config;

	/** Creates new form ServerMain */
	public ServerMain(ServerConfig config) {
		this.config=config;
		initComponents();
		postComponents();
		
	}

	private void postComponents() {
		jTextFieldDaemonHost.setText(config.getDaemonHost());
		jTextFielddaemonPort.setText(config.getDaemonPort().toString());
		jTextFieldgroupName.setText(config.getGroupName());
		jTextFieldprivateName.setText(config.getPrivateName());
		jTextFieldregstryHost.setText(config.getRegstryHost());
		jTextFieldregstryPort.setText(config.getRegstryPort().toString());
		jTextFieldserverName.setText(config.getServerName());
		jTextFieldserverPort.setText(config.getServerPort().toString());

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		 jLabel1 = new javax.swing.JLabel();
	        jTextFieldDaemonHost = new javax.swing.JTextField();
	        jLabel2 = new javax.swing.JLabel();
	        jTextFielddaemonPort = new javax.swing.JTextField();
	        jLabel3 = new javax.swing.JLabel();
	        jTextFieldprivateName = new javax.swing.JTextField();
	        jLabel4 = new javax.swing.JLabel();
	        jTextFieldgroupName = new javax.swing.JTextField();
	        jLabel5 = new javax.swing.JLabel();
	        jTextFieldserverPort = new javax.swing.JTextField();
	        jLabel6 = new javax.swing.JLabel();
	        jTextFieldserverName = new javax.swing.JTextField();
	        jLabel7 = new javax.swing.JLabel();
	        jTextFieldregstryHost = new javax.swing.JTextField();
	        jLabel8 = new javax.swing.JLabel();
	        jTextFieldregstryPort = new javax.swing.JTextField();
	        jButton1 = new javax.swing.JButton();
	        jButton2 = new javax.swing.JButton();
	        jLabelPrimaryServer = new javax.swing.JLabel();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	        setResizable(false);
	        addWindowListener(new java.awt.event.WindowAdapter() {
	            public void windowClosing(java.awt.event.WindowEvent evt) {
	                beforWindowsClose(evt);
	            }
	        });

	        jLabel1.setText("daemonHost:");

	        jLabel2.setText("daemonPort:");

	        jLabel3.setText("privateName:*");

	        jLabel4.setText("groupName:");

	        jLabel5.setText("serverPort:*");

	        jLabel6.setText("serverName:*");

	        jLabel7.setText("regstryHost:");

	        jLabel8.setText("regstryPort:");

	        jButton1.setText("Start Server");
	        jButton1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton1ActionPerformed(evt);
	            }
	        });

	        jButton2.setText("Stop Server");
	        jButton2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton2ActionPerformed(evt);
	            }
	        });

	        jLabelPrimaryServer.setText("Primary Server:");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addComponent(jLabelPrimaryServer, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
	                        .addContainerGap())
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jLabel2)
	                            .addComponent(jLabel3)
	                            .addComponent(jLabel4)
	                            .addComponent(jLabel5)
	                            .addComponent(jLabel6)
	                            .addComponent(jLabel7)
	                            .addComponent(jLabel8)
	                            .addComponent(jLabel1))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jTextFieldDaemonHost, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFielddaemonPort, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldprivateName, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldgroupName, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldserverPort, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldserverName, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldregstryHost, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
	                            .addComponent(jTextFieldregstryPort, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
	                        .addContainerGap())
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(46, 46, 46)
	                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(39, 39, 39))))
	        );

	        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jLabelPrimaryServer, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel1)
	                    .addComponent(jTextFieldDaemonHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel2)
	                    .addComponent(jTextFielddaemonPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel3)
	                    .addComponent(jTextFieldprivateName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel4)
	                    .addComponent(jTextFieldgroupName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                    .addComponent(jLabel5)
	                    .addComponent(jTextFieldserverPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel6)
	                    .addComponent(jTextFieldserverName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel7)
	                    .addComponent(jTextFieldregstryHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel8)
	                    .addComponent(jTextFieldregstryPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jButton1)
	                    .addComponent(jButton2))
	                .addContainerGap())
	        );

	        pack();
	}// </editor-fold>//GEN-END:initComponents

	private void beforWindowsClose(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_beforWindowsClose
		if (server != null)
			server.stopServer();
		else
			System.exit(1);
	}// GEN-LAST:event_beforWindowsClose

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		 config = getConfig();
		if (config == null) {
			return;
		}
		try {
			server = new ServerImpl(config,this);
			this.setPrimaryServer(server.primaryServerName);
			jButton1.setEnabled(false);
			jTextFieldDaemonHost.setEditable(false);
			jTextFielddaemonPort.setEditable(false);
			jTextFieldgroupName.setEditable(false);
			jTextFieldprivateName.setEditable(false);
			jTextFieldregstryHost.setEditable(false);
			jTextFieldregstryPort.setEditable(false);
			jTextFieldserverName.setEditable(false);
			jTextFieldserverPort.setEditable(false);
			
			
			
			
		} catch (ServerSpreadException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		} catch (ServerRemoteException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}// GEN-LAST:event_jButton1ActionPerformed

	public void setPrimaryServer(String primaryServer){
		jLabelPrimaryServer.setText("Primary Server:"+primaryServer);
		this.setTitle(config.getServerName()
				+ "--"
				+ (server.IsPrimaryServer() ? "Primary Server"
						: "Backup Server"));
	}
	
	private ServerConfig getConfig() {
		ServerConfig config = new ServerConfig();
		try {
			config.setDaemonHost(jTextFieldDaemonHost.getText());
			config.setDaemonPort(Integer
					.valueOf(jTextFielddaemonPort.getText()));
			config.setGroupName(jTextFieldgroupName.getText());
			config.setPrivateName(jTextFieldprivateName.getText());
			config.setRegstryHost(jTextFieldregstryHost.getText());
			config.setRegstryPort(Integer.valueOf(jTextFieldregstryPort
					.getText()));
			config.setServerName(jTextFieldserverName.getText());
			config.setServerPort(Integer
					.valueOf(jTextFieldserverPort.getText()));
			return config;
		} catch (Exception e) {
			return null;
		}

	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		if (server != null) {
			server.stopServer();
			this.dispose();
			System.exit(0);
		} else
			System.exit(1);
	}// GEN-LAST:event_jButton2ActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ServerMain(ServerConfig.load()).setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JTextField jTextFieldDaemonHost;
	private javax.swing.JTextField jTextFielddaemonPort;
	private javax.swing.JTextField jTextFieldgroupName;
	private javax.swing.JTextField jTextFieldprivateName;
	private javax.swing.JTextField jTextFieldregstryHost;
	private javax.swing.JTextField jTextFieldregstryPort;
	private javax.swing.JTextField jTextFieldserverName;
	private javax.swing.JTextField jTextFieldserverPort;
	private javax.swing.JLabel jLabelPrimaryServer;
	// End of variables declaration//GEN-END:variables

}
